package com.winchain.chainbill.bill.biz.receive.rx;

import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.file.utils.FileHelper;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component
public class BillRcvFileUploadSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<CbRcv>> {
	public void onApplicationEvent(BillEvent<CbRcv> t) {
		CbRcv rcv = (CbRcv) t.getBill().getSource();
		if (rcv != null) {
			String fileIds = (String) t.getValue("fileIds");
			String contracts = (String) t.getValue("contracts");
			String invoices = (String) t.getValue("invoices");
			if (StringUtil.notEmpty(fileIds)) {
				FileHelper.setRelateId(fileIds, rcv.getRcvId(), "4");
			}
			if (StringUtil.notEmpty(contracts)) {
				try {
					List<Map> ms = JsonUtil.fromJsonArray(contracts, Map.class);
					for (Map m : ms) {
						FileHelper.setRelateId((String) m.get("fileId"), (String) m.get("relateId"), "4");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (StringUtil.notEmpty(invoices)) {
				try {
					List<Map> ms = JsonUtil.fromJsonArray(invoices, Map.class);
					for (Map m : ms) {
						FileHelper.setRelateId((String) m.get("fileId"), (String) m.get("relateId"), "6");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
